let access_database = require("../database/access_database")
let adminSQLStore = require("../database/SQLSTORE_admin")
let message = require("./message")
let code_message = require("./JsonFileOprate")
let crypto = require("crypto")
let secret = "aoligei"
module.exports = {

    adminSignIn: async function (req, res) {
        if (req.body.adminid) {
            if (req.body.password) {
                try {
                    let results = await access_database(adminSQLStore.get("selectAdminPasswordByID"), [req.body.adminid])
                    if (results.length > 0) {
                        let inputPasswordCripto = crypto.createHmac('sha256', secret).update(req.body.password).digest('hex')
                        if (results[0].password) {
                            if (inputPasswordCripto == results[0].password) {
                                req.session.adminid = req.body.adminid
                                let results = await access_database(adminSQLStore.get("selectAdminNameByID"), [req.body.adminid])
                                let name = results[0].name
                                req.session.adminname = name
                                res.json(new message(207, {"adminname": name}, eval(code_message["207"])))
                            } else {
                                res.json(new message(206, null, code_message["206"]))
                            }


                        }
                    } else {
                        res.json(new message(205, null, code_message["205"]))
                    }
                } catch (e) {
                    console.log(e)
                    res.json(new message(204, null, code_message["204"]))
                }
            } else {
                res.json(new message(203, null, code_message["203"]))
            }
        } else {
            res.json(new message(202, null, code_message["202"]))

        }
    },
    adminLogOut: function (req, res) {
        if (!req.session.adminid) {
            res.json(new message(449, null, code_message['449']))
        } else {
            console.log("即将退出的id" + req.session.adminname)
            req.session.destroy()
            res.json(new message(210, null, code_message['210']))
        }
    }


}